<template>
  <div class="login-container">
    <!-- 登录表单区域 -->
    <div class="login-box">
      <h2 class="login-subtitle">用户登录</h2>
      
      <a-form
        :model="loginForm"
        :rules="loginRules"
        @finish="handleLogin"
        layout="vertical"
      >
        <a-form-item label="手机号" name="phone">
          <a-input
            v-model:value="loginForm.phone"
            placeholder="请输入手机号"
            size="large"
          >
            <template #prefix>
              <MobileOutlined />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item label="密码" name="password">
          <a-input-password
            v-model:value="loginForm.password"
            placeholder="请输入密码"
            size="large"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            block
            :loading="loading"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>

      <div class="login-footer">
        <a-button type="link" @click="goToRegister">
          还没有账号？立即注册
        </a-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { LockOutlined, MobileOutlined } from '@ant-design/icons-vue'
import { loginWithPassword } from '../api/auth'

const router = useRouter()
const loading = ref(false)

// 登录表单
const loginForm = reactive({
  phone: '',
  password: ''
})

const loginRules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 手机号密码登录
const handleLogin = async () => {
  loading.value = true
  try {
    const result = await loginWithPassword(loginForm.phone, loginForm.password)
    if (result.code === 1) {
      message.success(result.msg || '登录成功')
      router.push('/')
    } else {
      message.error(result.msg || '登录失败')
    }
  } catch (error) {
    message.error(error.response?.data?.msg || '登录失败，请检查手机号和密码')
  } finally {
    loading.value = false
  }
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url('/src/assets/image/shamo.png') no-repeat center center;
  background-size: cover;
  position: relative;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 0;
}

/* 顶部标题栏样式 */
.login-header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px 0;
  width: 100%;
  position: relative;
  z-index: 10;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.sztu-icon {
  width: 80px;
  height: 80px;
}

.system-title {
  color: #003366;
  font-size: 2rem;
  font-weight: 700;
  margin: 0;
  font-family: 'Segoe Script', 'Brush Script MT', cursive;
}

/* 登录表单区域 */
.login-box {
  width: 450px;
  padding: 45px 50px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  position: relative;
  z-index: 10;
}

.login-subtitle {
  text-align: center;
  margin-bottom: 35px;
  font-size: 26px;
  font-weight: 600;
  color: #003366;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.5);
}



.login-footer {
  margin-top: 25px;
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.login-footer :deep(.ant-btn-link) {
  color: #1890ff;
  font-size: 14px;
}

.login-footer :deep(.ant-btn-link:hover) {
  color: #40a9ff;
}

/* 表单项样式优化 */
:deep(.ant-form-item-label > label) {
  font-weight: 500;
  color: #333;
}

:deep(.ant-input-affix-wrapper),
:deep(.ant-input) {
  border-radius: 6px;
}

:deep(.ant-btn-primary) {
  height: 44px;
  font-size: 16px;
  border-radius: 6px;
  background: linear-gradient(135deg, #003366 0%, #004d99 100%);
  border-color: #003366;
  box-shadow: 0 4px 12px rgba(0, 51, 102, 0.3);
  transition: all 0.3s ease;
}

:deep(.ant-btn-primary:hover) {
  background: linear-gradient(135deg, #004d99 0%, #0066cc 100%);
  border-color: #004d99;
  box-shadow: 0 6px 16px rgba(0, 51, 102, 0.4);
  transform: translateY(-2px);
}
</style>
